package com.lontyu.jsoup.utils;

import com.lontyu.jsoup.entity.Book;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: xzj
 * @Dscription：
 * @Date: Created by 10:16 2018/9/14
 */
public class JSoupUtil {

    public static List<Book> parseHtml(String html){
        List<Book> list = new ArrayList<>();
        Document doc = Jsoup.parse(html);
        Elements elements = doc.select("ul[class=gl-warp clearfix]").select("li[class=gl-item]");
        for (Element element: elements) {
            String jdId = element.attr("data-sku");
            String name = element.select("div[class=p-name]").select("em").text();
            String price = element.select("div[class=p-price]").select("strong").select("i").text();
            list.add( new Book(name, BigDecimal.valueOf(Double.parseDouble(price)), jdId));
        }
        return list;
    }
}
